const router = require("koa-router")();
const DB = require("../../../module/db");
const sd = require("silly-datetime");
const fs = require('fs');

router.get("/", async (ctx) => {
    ctx.body = "后台视频首页";

}).get("/getPicture", async (ctx) => {
    // console.log(ctx.query["atlasId"]);
    // 5f56f6a251c34a79b83cfd6b
    let filename = ctx.query["fileId"];
    let type = ctx.query["type"]
    let data = await DB.downloadFile(DB.getObjectId(ctx.query["fileId"]), '/tmp/'+filename+'.'+type);
    if (data !== 1){
        ctx.body = 0;
    } else {
        const readfilePromise = new Promise((resolve, reject) => {
            fs.readFile('/tmp/'+filename+'.'+type, function(err, data){
                if (err)
                    return reject(err);
                resolve(data)
            })
        });
        await readfilePromise.then(data => ctx.body = data, err => console.log(err));
    }

});


module.exports = router.routes();
